package com.hans.scala.ImplicitConversion

/**
 * Created by Juntao.Zhang on 2014/4/28.
 */
object Euclid extends App {

  /**
   * 最大公约数
   * @param i
   * @param j
   * @return
   */
  def gcd(i: Int, j: Int): Int = {
    var a = i.max(j)
    var b = i.min(j)
    while (b != 0) {
      val t = b
      b = a % t
      a = t
    }
    a
  }

  println(gcd(100, 30))
  println(gcd(30, 100))
  println(gcd(12, 3732))
  println(gcd(1, 3732))
  println(gcd(25, 3732))

}

class Fraction(val num: Int, val den: Int) extends Ordered[Fraction] {
  lazy val toDouble = num.toDouble / den

  override def toString = num + "/" + den

  def compare(that: Fraction) =
    if (this.toDouble > that.toDouble) 1
    else if (this.toDouble < that.toDouble) -1
    else 0

}

object Fraction extends App {
  def apply(num: Int, den: Int): Fraction = {
    new Fraction(num, den)
  }

  def smaller[T](a: T, b: T)(implicit order: T => Ordered[T]) = if (a < b) a else b

  println(smaller(Fraction(4, 5), Fraction(3, 4)))


}
